package com.gopay.common.cps.service.fund.impl;

import com.gopay.common.cps.dao.fund.FundCpsMainDao;
import com.gopay.common.cps.dao.fund.FundCpsOrderDao;
import com.gopay.common.cps.dao.fund.FundCpsPayDAO;
import com.gopay.common.cps.service.fund.FundOrderService;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.CpsGenPayOrder;
import com.gopay.common.domain.fund.FundGopayCpsGenOrder;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 订单操作工具类
 *
 * @author dong-wang5@hnair.com
 * @date 2017-12-28 19:38:31
 */
@Service("fundOrderService")
public class FundOrderServiceImpl implements FundOrderService {

    @Autowired
    private FundCpsMainDao fundCpsMainDaoExt;

    @Autowired
    private FundCpsOrderDao fundCpsOrderDao;

    @Autowired
    private FundCpsPayDAO fundCpsPayDAO;

    @Override
    public FundGopayCpsGenOrder getFundOrderByGopayOrderId(String gopayOrderId) {
        return fundCpsOrderDao.getFundGopayOrder(gopayOrderId);
    }

    @Override
    public CpsGenMainOrder getCpsGenMainOrderByGopayOrderId(String gopayOrderId) {
        return fundCpsMainDaoExt.find(CpsGenMainOrder.class, gopayOrderId);
    }

    @Override
    public List<CpsGenMainOrder> getCpsGenMainOrder4Settle(String gopayOrderId){
        return fundCpsMainDaoExt.getCpsGenMainOrder4Settle(gopayOrderId);
    }

    @Override
    public List<CpsGenMainOrder> getCpsGenMainOrder4Settle(String gopayOrderId,String intTxnCd){
        DetachedCriteria criteria = DetachedCriteria.forClass(CpsGenMainOrder.class);
        criteria.add(Restrictions.eq("remark2",gopayOrderId));
        criteria.add(Restrictions.eq("gopayIntTxnCd",intTxnCd));
        criteria.add(Restrictions.eq("txnStaCd",30000));
        return fundCpsMainDaoExt.findByCriteria(criteria);
    }

    @Override
    public CpsGenPayOrder getCpsGenPayOrderByGopayOrderId(String gopayOrderId) {
        return fundCpsPayDAO.find(CpsGenPayOrder.class, gopayOrderId);
    }

    @Override
    public void updateFundCpsOrderTypeByGopayOrderId(String gopayId, String orderType) {
        fundCpsOrderDao.updateFundCpsMainOrderTypeByGopayOrderId(gopayId, orderType);
    }

    @Override
    public void updatePayOrders(CpsGenPayOrder pay) {
        pay.setLastUpdTs(new Date());
        fundCpsPayDAO.update(pay);
    }

    @Override
    public void updateMainOrder(CpsGenMainOrder mainOrder){
        mainOrder.setLastUpdTs(new Date());
        fundCpsMainDaoExt.update(mainOrder);
    }

    @Override
    public void updateFundOrder(FundGopayCpsGenOrder fundOrder) {
        fundOrder.setLastUpdTs(new Date());
        fundCpsOrderDao.update(fundOrder);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    public void updateFundAndMainOrderByGopayOrderId(String gopayOrderId,String fundOrderType,int txnStaCd) {
        FundGopayCpsGenOrder fundGopayCpsGenOrder = getFundOrderByGopayOrderId(gopayOrderId);
        CpsGenMainOrder cpsGenMainOrder = getCpsGenMainOrderByGopayOrderId(gopayOrderId);

        fundGopayCpsGenOrder.setOrderType(fundOrderType);
        cpsGenMainOrder.setTxnStaCd(txnStaCd);

        updateMainOrder(cpsGenMainOrder);
        updateFundOrder(fundGopayCpsGenOrder);
    }

}
